﻿using Catalogo.Portable.DataSources;
using Catalogo.Portable.Model;
using Catalogo.Portable.ViewModel.Commands;
using Catalogo.Portable.ViewModel.Messages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml;

namespace Catalogo.Portable.ViewModel
{
    public class ItemPageViewModel : ViewModelBase
    {
        public ICommand AbrirHomeCommand { get; private set; }

        private Item _Item;
        public Item Item
        {
            get { return _Item; }
            set { SetProperty(ref this._Item, value); }
        }

        private Categoria _Categoria;
        public Categoria Categoria
        {
            get { return _Categoria; }
            set { SetProperty(ref this._Categoria, value); }
        }

        private Empresa _Empresa;
        public Empresa Empresa
        {
            get { return _Empresa; }
            set { SetProperty(ref this._Empresa, value); }
        }

        public ItemPageViewModel()
        {
            AbrirHomeCommand = new DelegateCommand(OnAbrirHomeCommand);
        }

        private void OnAbrirHomeCommand(object obj)
        {
            PublishMessage<NavegarParaPagina>(new NavegarParaPagina("HomePage", obj));
        }

        public async Task LoadAsync(string idItem)
        {
            Empresa = await CatalogoDataSource.Instance.ObterEmpresaAsync();
            
            var item = await CatalogoDataSource.Instance.ObterItemAsync(idItem);
            if (item != null)
            {
                Categoria = item.Categoria;
            }
            Item = item;
        }

        public Uri ObterUriParaCompartilhar()
        {
            if (Item != null && !String.IsNullOrEmpty(Item.Uri))
            {
                return new Uri(Item.Uri);
            }

            return new Uri(Empresa.Website);
        }

        public async Task<string> ObterHtmlParaCompartilharAsync()
        {
            var stringBuilder = new StringBuilder();
            var settings = new XmlWriterSettings() {  OmitXmlDeclaration= true };
            using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
            {
                xmlWriter.WriteStartElement("div");

                string empresaShareText = Empresa.ShareTexto.Replace("#TELEFONE#", Empresa.TodosTelefones).Replace("#URL#", Empresa.Website);

                xmlWriter.WriteElementString("p", empresaShareText);

                xmlWriter.WriteStartElement("hr");
                xmlWriter.WriteEndElement();

                //Dados do Item compartilhado
                if (Item != null)
                {
                    xmlWriter.WriteStartElement("img");
                    xmlWriter.WriteAttributeString("src", new Uri(new Uri("ms-appx:///"), Item.ImageUrl).ToString());
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteElementString("h2", Item.SubTitulo);

                    xmlWriter.WriteStartElement("br");
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteElementString("b", string.Format("R$ {0}", Item.Valor));

                    xmlWriter.WriteElementString("p", Item.Descricao);
                }

                xmlWriter.WriteEndElement();
                xmlWriter.Flush();
            }
            return stringBuilder.ToString();
        }


        public string ObterTituloParaCompartilhar()
        {
            if (Item == null || Empresa == null)
                return null;
            return string.Format("{0} - {1}", Empresa.Nome, Item.Nome);
        }
    }
}
